﻿@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_LayoutIndex.cshtml";
}
<script>
    var organizeid = '@LeSoft.Application.Code.OperatorProvider.Provider.Current().CompanyId';
    var _State = "", _Category = "";
    $(function () {
        InitialPage();
        GetGrid();
    })
    function InitialPage() {
        //layout布局
        $('#layout').layout({
            applyDemoStyles: true,
            onresize: function () {
                $(window).resize()
            }
        });
        $(window).resize(function (e) {
            window.setTimeout(function () {
                $('#gridTable').setGridWidth(($('.gridPanel').width()));
                $("#gridTable").setGridHeight($(window).height() - 181.5);

            }, 200);

            e.stopPropagation();

        });
        //房源状态
        $("#State").ComboBox({
            //  url: "
            data: [{ "key": "1", "value": "待提交" }, { "key": "2", "value": "待审批" }, { "key": "3", "value": "已完成" }, { "key": "5", "value": "已取消" }],
            id: "key",
            text: "value",
            description: "=选择订单状态=",
            height: "200px",
            click: function (item) {
                _State = item.id;
                SearchEvent();
            }
        });
        //业务类型
        $("#Category").ComboBox({
            //  url: "
            data: [{ "key": "1", "value": "发布" }, { "key": "2", "value": "撤回" }],
            id: "key",
            text: "value",
            description: "=选择业务类型=",
            height: "150px",
            click: function (item) {
                _Category = item.id;
                SearchEvent();
            }
        });
        //初始化时间
        $("#StartTime").val("@DateTime.Now.AddDays(-DateTime.Now.Day+1).ToString("yyyy-MM-dd")");
        $("#EndTime").val("@DateTime.Now.AddMonths(1).AddDays(- DateTime.Now.Day).ToString("yyyy-MM-dd")");
        $("#time_horizon").ComboBox({
            data: [{ "key": 2, "value": "上周" }, { "key": 1, "value": "本周" }, { "key": 4, "value": "上月" }, { "key": 3, "value": "本月" }, { "key": 5, "value": "本年" }],
            id: "key",
            text: "value",
            description: "=选择=",
            click: function (item) {

                var value = item.attr('data-value');
                switch (value) {
                    case "1"://本周
                        $("#StartTime").val("@DateTime.Now.AddDays(1 - DateTime.Now.DayOfWeek.GetHashCode()).ToString("yyyy-MM-dd")");
                        $("#EndTime").val("@DateTime.Now.AddDays(7 - DateTime.Now.DayOfWeek.GetHashCode()).ToString("yyyy-MM-dd")");
                        break;
                    case "2"://上周
                        $("#StartTime").val("@DateTime.Now.AddDays(1 - DateTime.Now.DayOfWeek.GetHashCode()-7).ToString("yyyy-MM-dd")");
                        $("#EndTime").val("@DateTime.Now.AddDays(7 - DateTime.Now.DayOfWeek.GetHashCode()-7).ToString("yyyy-MM-dd")");
                        break;
                    case "3"://本月
                        $("#StartTime").val("@DateTime.Now.AddDays(-DateTime.Now.Day+1).ToString("yyyy-MM-dd")");
                        $("#EndTime").val("@DateTime.Now.AddMonths(1).AddDays(- DateTime.Now.Day).ToString("yyyy-MM-dd")");
                        break;
                    case "4"://上月
                        $("#StartTime").val("@DateTime.Now.AddDays(-DateTime.Now.Day + 1).AddMonths(-1).ToString("yyyy-MM-dd")");
                        $("#EndTime").val("@DateTime.Now.AddDays(-DateTime.Now.Day).ToString("yyyy-MM-dd")");
                        break;
                    case "5"://本年
                        $("#StartTime").val("@DateTime.Now.AddDays(-DateTime.Now.DayOfYear + 1).ToString("yyyy-MM-dd")");
                        $("#EndTime").val("@DateTime.Now.AddMonths(12 - DateTime.Now.Month + 1).AddDays(-DateTime.Now.Day).ToString("yyyy-MM-dd")");
                        break;
                    default:
                        break;
                }
                $("#SelectedStartTime").html($("#StartTime").val());
                $("#SelectedEndTime").html($("#EndTime").val());
                SearchEvent();
            }
        });
        $("#time_horizon").ComboBoxSetValue(3);

    }

    function GetGrid() {
        var selectedRowIndex = 0;
        $gridTable = $("#gridTable");
        var queryJson = {
            OrganizeId: organizeid,
            State: $("#State").attr('data-value'),
            Category: $("#Category").attr('data-value'),
            StartTime: $("#StartTime").val(),
            EndTime: $("#EndTime").val() + " 23:59:59",
            Code: $("#Code").val(),
        };
        $gridTable.jqGrid({
            url: "../../ResourceManage/RS_PStructPublish/GetPagList",
            postData: { queryJson: JSON.stringify(queryJson) },
            datatype: "json",
            height: $(window).height() - 141,
            autowidth: true,
            colModel: [
                { label: "主键", name: "id", hidden: true },
                {
                    label: "业务类型", name: "category", index: "category", width: 200, align: "center", sortable: false,
                    formatter: function (cellvalue) {
                        var Msg = "";
                        switch (cellvalue) {
                            case "1": Msg = "发布";
                                break;
                            case "2": Msg = "撤回";
                                break;
                            default: Msg = "---";
                                break;
                        }
                        return Msg;
                    }
                },
                { label: "单据编号", name: "code", index: "code", width: 200, align: "center", sortable: false },
                { label: "单据时间", name: "date", index: "date", width: 200, align: "center", sortable: false },
                { label: "创建人", name: "createusername", index: "createusername", width: 200, align: "center", sortable: false },
                 {
                     label: "状态", name: "state", index: "state", width: 200, align: "center", sortable: false,
                     formatter: function (cellvalue, options, rowObject) {

                         var Msg = "";
                         switch (cellvalue) {
                             case 1: Msg = "<span  style='background-color:#ff6a00' class='label '>待提交</span>";
                                 break;
                             case 2: Msg = "<span  style='background-color:#8FD0EE' class='label '>待审核</span>";
                                 break;
                             case 3: Msg = "<span  style='background-color:#D27820' class='label '>审核完成</span>";
                                 break;
                             case 4:
                                 if (rowObject["category"] == "1") {
                                     Msg = "<span  class='label label-success'>已发布</span>";
                                 }
                                 if (rowObject["category"] == "2") {
                                     Msg = "<span  class='label label-success'>已撤回</span>";
                                 }

                                 break;
                             case 5: Msg = "<span  style='background-color:#ccc' class='label '>已取消</span>";
                                 break;
                             case 6:
                                 if (rowObject["category"] == "1") {
                                     Msg = "<span style='background-color:#f00' class='label '>发布失败</span>";
                                 }
                                 if (rowObject["category"] == "2") {
                                     Msg = "<span  style='background-color:#f00' class='label '>撤回失败</span>";
                                 } 
                                 break;
                             case 7: Msg = "<span  style='background-color:#f00' class='label '>审核不同意</span>";
                                 break;
                             default: Msg = cellvalue;
                                 break;
                         }
                         return Msg;
                     }
                 },
                { label: "审核人", name: "checkman", index: "checkman", width: 200, align: "center", sortable: false },
                { label: "审核时间", name: "checkdate", index: "checkdate", width: 200, align: "center", sortable: false },
                { label: "审核说明", name: "checkmemo", index: "checkmemo", width: 200, align: "center", sortable: false },
                { label: "备注", name: "description", index: "description", width: 200, align: "center", sortable: false },
            ],
            viewrecords: true,
            rowNum: 30,
            rowList: [30, 50, 100, 500, 1000],
            pager: "#gridPager",
            sortname: 'date',
            sortorder: 'desc',
            rownumbers: true,
            shrinkToFit: true,
            gridview: true,
            onSelectRow: function (status) {
                selectedRowIndex = $("#" + this.id).getGridParam('selrow');
            },
            gridComplete: function () {
                $("#" + this.id).setSelection(selectedRowIndex, false);
            },
            subGrid: true,
            subGridRowExpanded: function (subgrid_id, row_id) {
                var keyValue = $gridTable.jqGrid('getRowData', row_id)['Id'];
                var subgrid_table_id = subgrid_id + "_t";
                $("#" + subgrid_id).html("<table id='" + subgrid_table_id + "'></table>");
                $("#" + subgrid_table_id).jqGrid({
                    url: "../../ResourceManage/RS_PStructPublish/GetPStructPublishBillDetailEntity",
                    postData: { keyValue: row_id },
                    datatype: "json",
                    height: "100%",
                    colModel: [
                   { label: "Id", name: "Id", hidden: true },
                   { label: "楼栋", name: "Pavilionion", index: "Pavilionion", width: 300, align: "left", sortable: false },
                   { label: "房屋名称", name: "RoomName", index: "RoomName", width: 150, align: "center", sortable: false },
                   { label: "房屋编号", name: "RoomCode", index: "RoomCode", width: 150, align: "center", sortable: false },
                   {
                      label: "房屋状态", name: "RoomStateStr", index: "RoomStateStr", width: 100, align: "center", sortable: false,
                      formatter: function (cellvalue, options, rowObject) {
                          switch (rowObject.RoomState) {
                              case "0":
                              case 0: return "准备房";
                              case "1":
                              case 1: return "待租";
                              case "2":
                              case 2: return "已选取";
                              case "3":
                              case 3: return "已租";
                              case "4":
                              case 4: return "自用";
                              case "5":
                              case 5: return "已售";
                              case "6":
                              case 6: return "续租中";
                              case "7":
                              case 7: return "维修中";
                              default: return "---";

                          }
                      }
                   },
                   { label: "月租金", name: "MonthlyRent", index: "MonthlyRent", width: 100, align: "center", sortable: false },               
                  {
                      label: "PC端操作状态", name: "IsPublish", index: "IsPublish", width: 50, align: "center",
                      formatter: function (cellvalue, options, rowObject) {
                          switch (cellvalue) {
                              case "0":
                              case 0: return "未发布"; break;
                              case 1:
                              case "1":
                              case "2":
                              case 2: return "已发布"; break;
                              case "3":
                              case 3: return "发布失败"; break;
                              case "4":
                              case 4: return "已撤回"; break;
                              case "5":
                              case 5: return "撤回失败"; break;
                              default: return rowObject.IsPublishToPC == 1 ? "操作异常" : ""; break;

                          }
                      },
                  },
                {
                    label: "其他平台操作状态", name: "IsPublishOther", index: "IsPublishOther", width: 50, align: "center",
                    formatter: function (cellvalue, options, rowObject) {
                        switch (cellvalue) {
                            case "0":
                            case 0: return "未发布"; break;
                            case 1:
                            case "1":
                            case "2":
                            case 2: return "已发布"; break;
                            case "3":
                            case 3: return "发布失败"; break;
                            case "4":
                            case 4: return "已撤回"; break;
                            case "5":
                            case 5: return "撤回失败"; break;
                            default: return rowObject.IsPublishToOther == 1 ? "操作异常" : ""; break;
                        }
                    },
                },
                {
                    label: "户型", name: "RoomType", index: "RoomType", width: 100, align: "center", sortable: false,
                    formatter: function (cellvalue, options, rowObject) {
                        switch (cellvalue) {
                            case "1": case 1: return "一室";
                            case "2": case 2: return "一室一厅";
                            case "3": case 3: return "二室";
                            case "4": case 4: return "二室一厅";
                            case "5": case 5: return "三室";
                            case "6": case 6: return "三室一厅";
                            case "7": case 7: return "四室";
                            case "8": case 8: return "五室";
                            default: return cellvalue;

                        }
                    }
                },
                { label: "建筑面积", name: "Area", index: "Area", width: 100, align: "center", sortable: false },
                { label: "起租日期", name: "EMoveInDate", index: "EMoveInDate", width: 100, align: "center", sortable: false, formatter: "date", formatoptions: { newformat: 'Y-m-d' } },
                 { label: "备注", name: "Description", index: "Description", width: 100, align: "center", sortable: false },
                    ],
                    caption: "单据明细",
                    rowNum: "1000",
                    rownumbers: true,
                    shrinkToFit: false,
                    gridview: true,
                    hidegrid: false
                });
            }
        });
        //查询事件
        $("#btn_Search").click(function () {
            SearchEvent();
            $('#btn_Reset').trigger("click");
            $(".ui-filter-text").trigger("click");
        });
        $("#btn_Reset").click(function () {
            $("#Code").val("");
        })
    }
    //查询表格函数
    function SearchEvent() {
        var queryJson = {
            OrganizeId: organizeid,
            State: $("#State").attr('data-value'),
            Category: $("#Category").attr('data-value'),
            StartTime: $("#StartTime").val(),
            EndTime: $("#EndTime").val() + " 23:59:59",
            Code: $("#Code").val(),
            PStruct: $("#PStruct").val()
        };
        $("#gridTable").jqGrid('setGridParam', {
            url: "../../ResourceManage/RS_PStructPublish/GetPagList",
            postData: { queryJson: JSON.stringify(queryJson) },
        }).trigger('reloadGrid');
    }
    function btn_add() {
        dialogOpen({
            id: "Form",
            title: '新增单据',
            url: '/ResourceManage/RS_PStructPublish/Form',
            width: "1200px",
            height: "750px",
            callBack: function (iframeId) {
                top.frames["Form"].AcceptClick();
            },
            btn: null
        })
    }
    function btn_edit() {
        var keyValue = $("#gridTable").jqGridRowValue("id");
        if (checkedRow(keyValue)) {
            dialogOpen({
                id: "Form",
                title: '编辑单据',
                url: '/ResourceManage/RS_PStructPublish/Form?keyValue=' + keyValue,
                width: "1200px",
                height: "750px",
                callBack: function (iframeId) {
                    top.frames["Form"].AcceptClick();
                },
                btn: null
            })
        }
    }
    function btn_delete() {
        var keyValue = $("#gridTable").jqGridRowValue("id");
        var State = $("#gridTable").jqGridRowValue("state");
        if (checkedRow(keyValue)) {
            if (State.indexOf("审核通过") > 0) {
                dialogMsg('已审核订单，无法删除！', 0);
                return false;
            }
            if (State.indexOf("已发布") > 0) {
                dialogMsg('已发布订单，无法删除！', 0);
                return false;
            }
            $.RemoveForm({
                url: '/ResourceManage/RS_PStructPublish/RemovePStructPublishBillEntity',
                param: { keyValue: keyValue },
                success: function (data) { 
                }
            })
        }
    }
    function btn_aidit() {
        var keyValue = $("#gridTable").jqGridRowValue("id");
        if (checkedRow(keyValue)) {
            var State = $("#gridTable").jqGridRowValue("state");
            if (State.indexOf("待提交") > 0) {
                dialogMsg('未提交，无法审核！', 0);
                return false;
            }
            if (State.indexOf("审核通过") > 0) {
                dialogMsg('已审核！', 0);
                return false;
            }
            if (State.indexOf("已发布") > 0) {
                dialogMsg('已发布！', 0);
                return false;
            }
            if (State.indexOf("审核不通过") > 0) {
                dialogMsg('审核已结束！', 0);
                return false;
            }
            if (State.indexOf("已撤回") > 0) {
                dialogMsg('已撤回！', 0);
                return false;
            }
            if (State == "---") {
                dialogMsg('状态未知，无法审核！', 0);
                return false;
            }
            dialogOpen({
                id: "AiditForm",
                title: '审核',
                url: '/ResourceManage/RS_PStructPublish/AiditForm?keyValue=' + keyValue,
                width: "450px",
                height: "350px",
                callBack: function (iframeId) {
                    top.frames["Form"].AcceptClick();
                },
                btn: null
            })
        }
    }
</script>
<div class="ui-layout" id="layout" style="height: 100%; width: 100%;">

    <div class="ui-layout-center">
        <div class="center-Panel">
            <div class="titlePanel">
                <div class="title-search">
                    <table>
                        <tr>
                            <td>查询条件</td>
                            <td style="padding-left: 10px;">
                                <div class="ui-filter" style="width: 200px;  ">
                                    <div class="ui-filter-text">
                                        <strong id="SelectedStartTime">@DateTime.Now.AddDays(-DateTime.Now.Day + 1).ToString("yyyy-MM-dd")</strong> 至 <strong id="SelectedEndTime">@DateTime.Now.AddMonths(1).AddDays(-DateTime.Now.Day).ToString("yyyy-MM-dd")</strong>
                                    </div>
                                    <div class="ui-filter-list" style="width: 350px;">
                                        <table class="form" id="filter-form">
                                            <tr>
                                                <th class="formTitle">发布时间：</th>
                                                <td class="formValue">
                                                    <div style="float: left; width: 45%;">
                                                        <input id="StartTime" readonly type="text" value="" class="form-control input-wdatepicker" onfocus="WdatePicker()">
                                                    </div>
                                                    <div style="float: left; width: 10%; text-align: center;">至</div>
                                                    <div style="float: left; width: 45%;">
                                                        <input id="EndTime" readonly type="text" value="2017-07-31" class="form-control input-wdatepicker" onfocus="WdatePicker()">
                                                    </div>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td class="formTitle">单据编号：</td>
                                                <td class="formValue">
                                                    <input id="Code" maxlength="20" type="text" class="form-control">
                                                </td>
                                            </tr>
                                            <tr>
                                                <td class="formTitle">房产信息：</td>
                                                <td class="formValue">
                                                    <input id="PStruct" maxlength="20" type="text" class="form-control">
                                                </td>
                                            </tr>

                                        </table>
                                        <div class="ui-filter-list-bottom">
                                            <a id="btn_Reset" class="btn btn-default">&nbsp;重&nbsp;&nbsp;置</a>
                                            <a id="btn_Search" class="btn btn-primary">&nbsp;查&nbsp;&nbsp;询</a>
                                        </div>
                                    </div>
                                </div>
                            </td>
                            <td style="padding-left: 10px;">
                                <div id="time_horizon" type="selectTree" class="ui-select" style="width: 60px;"></div>
                            </td>
                            <td style="padding-left: 10px;">
                                <div id="State" type="select" class="ui-select" style="width: 120px;"></div>
                            </td>
                            <td style="padding-left: 10px;">
                                <div id="Category" type="select" class="ui-select" style="width: 120px;"></div>
                            </td>

                        </tr>

                    </table>

                </div>

                <div class="toolbar">
                    <div class="btn-group">
                        <a id="lr-replace" class="btn btn-default" onclick="reload(); "><i class="fa fa-refresh"></i>&nbsp;刷新</a>
                        <a id="lr-add" class="btn btn-default" onclick="btn_add()"><i class="fa fa-plus"></i>&nbsp;新增</a>
                        <a id="lr-edit" class="btn btn-default" onclick="btn_edit()"><i class="fa fa-pencil-square-o"></i>&nbsp;编辑</a>
                        <a id="lr-delete" class="btn btn-default" onclick="btn_delete()"><i class="fa fa-trash-o"></i>&nbsp;删除</a>
                        <a id="lr-audit" class="btn btn-default" onclick="btn_aidit()">&nbsp;审核</a>
                    </div>
                    <script>$('.toolbar').authorizeButton()</script>
                </div>
            </div>


            <div class="gridPanel">
                <table id="gridTable"></table>
                <div id="gridPager"></div>
            </div>

        </div>
    </div>

</div>


